import type { App } from 'vue';
import type { RouteRecordRaw } from 'vue-router';

import { createRouter, createWebHashHistory } from 'vue-router';

// create router
export const router = createRouter({
  history: createWebHashHistory(),
  routes: [
    {
      path: '/',
      redirect: '/home',
      component: () => import('@/App.vue'),
      children: [
        {
          path: '/home',
          name: 'Home',
          component: () => import('@/views/home/index.vue'),
        },
        {
          path: '/login',
          redirect: '/login',
          component: () => import('@/views/login/index.vue'),
          children: [
            {
              path: '/login',
              name: 'Login',
              component: () => import('@/views/login/login/index.vue'),
            },
            {
              path: '/register',
              name: 'Register',
              component: () => import('@/views/login/register/index.vue'),
            },
            {
              path: '/forget',
              name: 'Forget',
              component: () => import('@/views/login/forget/index.vue'),
            },
          ],
        },
      ],
    },
  ] as unknown as RouteRecordRaw[],
  strict: true,
  scrollBehavior: () => ({ left: 0, top: 0 }),
});

// reset router
export function resetRouter() {
  router.getRoutes().forEach(route => {
    const { name } = route;
    if (name) {
      router.hasRoute(name) && router.removeRoute(name);
    }
  });
}

export function setupRouter(app: App<Element>) {
  app.use(router);
}
